<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=utf-8" errorPage="/error.jsp" isELIgnored="false" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://www.opensymphony.com/oscache" prefix="cache" %>
<%@ taglib uri="http://atleap.blandware.com/taglib" prefix="atleap" %>
<html:xhtml />
<script type="text/javascript">
    function confirmRestore() {
        return confirm('<atleap:message key="core.backup.confirmRestore" />');
    }
</script>

<atleap:constants className="com.blandware.atleap.webapp.util.core.WebappConstants" var="GRID_PAGE_SIZE" />
<atleap:globalProperty name="${pageScope.GRID_PAGE_SIZE}" defaultValue="10" type="integer" var="pageSize" />

<atleap:globalProperty name="dbbackup.download" var="downloadAllowed"/>
<atleap:globalProperty name="dbbackup.upload" var="uploadAllowed"/>

<atleap:isUserInRole roles="core-backup-backup" var="canBackup" />
<atleap:isUserInRole roles="core-backup-restore" var="canRestore" />
<atleap:isUserInRole roles="core-backup-download" var="canDownload" />
<atleap:isUserInRole roles="core-backup-upload" var="canUpload" />

<c:set var="canDownload" value="${downloadAllowed == 'true' && canDownload}"/>
<c:set var="canUpload" value="${uploadAllowed == 'true' && canUpload}"/>

<c:set var="infoCells" value="1" />
<c:set var="constantButtonCells" value="0" />
<c:set var="additionalCells" value="0" />
<c:if test="${canRestore}">
	<c:set var="additionalCells" value="${additionalCells+1}" />
</c:if>
<c:if test="${canDownload}">
	<c:set var="additionalCells" value="${additionalCells+1}" />
</c:if>

<atleap:content identifier="information" ignore="true" />
<atleap:grid name="backupArchiveGrid" pageSize="${pageSize}">
    <atleap:messages property="cannotList" />
    <atleap:messages property="cannotRestore" />
    <atleap:messages property="cannotBackup" />
    <atleap:messages property="success" />
    <c:if test="${requestScope.canListArchives}">
        <table border="0" cellpadding="0" cellspacing="0" class="grid">
            <thead>
                <tr>
                    <th nowrap="nowrap">
                        <atleap:message key="core.backup.date" />
                    </th>
                    <c:if test="${constantButtonCells + additionalCells > 0}">
                        <th colspan="${constantButtonCells + additionalCells}">
                            <img src="<atleap:rewriteUrl href="/images/theme/${appConfig['adminCssTheme']}/spacer.gif" />" border="0"  />
                        </th>
                    </c:if>
                </tr>
            </thead>
            <tbody>
                <atleap:contextMenu var="menuId">
                    <atleap:rowsIterator beanId="backupManager" method="listArchives" var="date" oddClass="oddRow" evenClass="evenRow">
                        <atleap:itemSet>
                            <tr class="${rowClass}" oncontextmenu="return showContextMenu(${menuId}, event, ${indexId});">
                                <td class="defaultCursorCell"><atleap:formatDate value="${date}" type="datetime"/></td>
                                <atleap:formatDate value="${date}" var="isoDate" type="datetime"/>
                                <atleap:item includeInContextMenu="${canRestore}" includeInPage="${canRestore}">
                                    <atleap:message key="core.backup.buttons.restore" var="restoreMsg" />
                                    <td align="center" width="20" height="20" valign="middle" class="defaultCursorCell">
                                        <atleap:itemEnabled>
                                            <atleap:itemLink action="core/backup/restore" title="${restoreMsg}" transaction="true" confirmExpression="confirmRestore();">
                                                <atleap:param name="date" value="${isoDate}" />
                                                    <img src="<atleap:rewriteUrl href="/images/theme/${appConfig['adminCssTheme']}/restore.gif" />" border="0" title="${restoreMsg}" alt="${restoreMsg}" align="absmiddle" />
                                                    <atleap:itemLabel>
                                                        ${restoreMsg}
                                                    </atleap:itemLabel>
                                            </atleap:itemLink>
                                        </atleap:itemEnabled>
                                    </td>
                                </atleap:item>
                                <atleap:item includeInContextMenu="${canDownload}" includeInPage="${canDownload}">
                                    <atleap:message key="core.backup.buttons.download" var="downloadMsg" />
                                    <td align="center" width="20" height="20" valign="middle" class="defaultCursorCell">
                                        <atleap:itemEnabled>
                                            <atleap:itemLink href="/servlet/dbBackup" title="${downloadMsg}">
                                                <atleap:param name="date" value="${isoDate}" />
                                                    <img src="<atleap:rewriteUrl href="/images/theme/${appConfig['adminCssTheme']}/download.gif" />" border="0" title="${downloadMsg}" alt="${downloadMsg}" align="absmiddle" />
                                                    <atleap:itemLabel>
                                                        ${downloadMsg}
                                                    </atleap:itemLabel>
                                            </atleap:itemLink>
                                        </atleap:itemEnabled>
                                    </td>
                                </atleap:item>
                            </tr>
                        </atleap:itemSet>
                    </atleap:rowsIterator>
                </atleap:contextMenu>
                <atleap:noRows>
                    <tr>
                        <td class="tfoot" colspan="${infoCells + constantButtonCells + additionalCells}" align="center">
                            <em><atleap:message key="core.commons.nothingToShow"/></em>
                        </td>
                    </tr>
                </atleap:noRows>
            </tbody>
            <tfoot>
                <tr>
                    <atleap:pager pageCount="10">
                        <td colspan="${infoCells + constantButtonCells + additionalCells}" align="center">
                            <atleap:message key="core.grid.pager.title" />&nbsp;
                            <atleap:firstPage><img src="<atleap:rewriteUrl href="/images/theme/${appConfig['adminCssTheme']}/first.gif" />" border="0" alt="<atleap:message key="core.grid.pager.first"/>" title="<atleap:message key="core.grid.pager.first"/>"  /></atleap:firstPage>
                            <atleap:prevPage><img src="<atleap:rewriteUrl href="/images/theme/${appConfig['adminCssTheme']}/prev.gif" />" border="0" alt="<atleap:message key="core.grid.pager.prev"/>" title="<atleap:message key="core.grid.pager.prev"/>"  /></atleap:prevPage>
                            <atleap:pagesIterator>
                                <atleap:page>${pageNumber}&nbsp;</atleap:page>
                                <atleap:currentPage><b>${pageNumber}</b>&nbsp;</atleap:currentPage>
                            </atleap:pagesIterator>
                            <atleap:nextPage><img src="<atleap:rewriteUrl href="/images/theme/${appConfig['adminCssTheme']}/next.gif" />" border="0" alt="<atleap:message key="core.grid.pager.next"/>" title="<atleap:message key="core.grid.pager.next"/>"  /></atleap:nextPage>
                            <atleap:lastPage><img src="<atleap:rewriteUrl href="/images/theme/${appConfig['adminCssTheme']}/last.gif" />" border="0" alt="<atleap:message key="core.grid.pager.last"/>" title="<atleap:message key="core.grid.pager.last"/>"  /></atleap:lastPage>
                        </td>
                    </atleap:pager>
                </tr>
            </tfoot>
        </table>
    </c:if>

    <table>
        <tr>
            <c:if test="${canBackup}">
                <td>
                    <form onsubmit="disableButtons(this, true);window.location.href='<atleap:rewriteUrl action="/core/backup/backup" transaction="true" />';return false;">
                        <atleap:message key="core.backup.buttons.backup" var="performMsg" />
                        <input type="submit" class="button" value="${performMsg}" />
                    </form>
                </td>
            </c:if>
            <c:if test="${canRestore}">
                <td>
                    <form onsubmit="if(confirmRestore()){disableButtons(this, true);window.location.href='<atleap:rewriteUrl action="/core/backup/restore" transaction="true" />'};return false;">
                        <atleap:message key="core.backup.buttons.restoreInitial" var="performMsg" />
                        <input type="submit" class="button" value="${performMsg}" />
                    </form>
                </td>
            </c:if>
            <c:if test="${canUpload && canRestore}">
                <td>
                    <form onsubmit="disableButtons(this, true);window.location.href='<atleap:rewriteUrl action="/core/backup/callUpload" />';return false;">
                        <atleap:message key="core.backup.buttons.upload" var="performMsg" />
                        <input type="submit" class="button" value="${performMsg}" />
                    </form>
                </td>
            </c:if>
        </tr>
    </table>
</atleap:grid>
